# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = "restructuredtext en"

from .. import pilelib as stack
from .. import comparecards as cmp_cards


class TrumpFirst_Foundation(stack.SS_Foundation):
    """
    Foundation #5 must play cards 1st to allow other pile build a card.
    """

    def cardsBuild(self, from_stack, cards):
        """
        _cardsBuild(from_stack, cards) -> bool

        Build cards checking parent then pile #5 has more cards.

        :Parameters:
          from_stack: Stack
            Sending pile

          cards: tuple
            cards from sending pile

        :returns: accept card sequence?
        """
        if not super(TrumpFirst_Foundation, self).cardsBuild(from_stack,
                                                             cards):
            return False
        return cards[0]['rank'] < len(self.game.s['foundations'][4].cards)


class HighCardMethod(object):
    """
    Alternate base of KING or high Trump depend on base card played.
    """

    def cardsBuild(self, from_stack, cards):
        """
        _cardsBuild(from_stack, cards) -> bool

        Build cards by setting base_rank based on base_card of cards so a king
        or high trump is played as the base card then checking parent.

        :Parameters:
          from_stack: Stack
            Sending pile

          cards: tuple
            cards from sending pile

        :returns: accept card sequence?
        """
        self.cap['base_rank'] = cmp_cards.highestRank(self.game,
                                                      cards[0]['suit'])
        return super(HighCardMethod, self).cardsBuild(from_stack, cards)


class TrumpAC_RowStack(stack.SequenceRowStack):
    """
    Build suits in rank by alt color and trumps in rank alone.
    """

    def _sequence(self, cards):
        """
        _sequence(cards) -> bool

        Allow suit cards to play alt color, but allow trump cards to build in
        rank alone.

        :Parameters:
          cards: tuple
            Row's foundation card and cards from sending pile

        :returns: accept card sequence?
        """
        if (cmp_cards.isSameColor(cards[1:], color=2) and
            cmp_cards.isRankSequence(cards, dir=self.cap['dir'])):
            return True
        return cmp_cards.isAlternateColorSequence(cards, **self.cap)


class HighTrumpAC_RowStack(HighCardMethod, TrumpAC_RowStack):
    """
    Build in rank by alt color and trump by rank alone. Base KING or high
    Trump.
    """
    pass


class RK_SS_RowStack(stack.RK_RowStack):
    """
    Build in rank by same suit. Play in rank alone.
    """

    def _build_sequence(self, cards):
        return cmp_cards.isSameSuitSequence(cards, **self.cap)
    pass


class HighRK_SS_RowsStack(RK_SS_RowStack):
    """
    Build in rank by same suit. Play in rank alone. Base KING or high Trump.
    """
    pass


class TrumpOnly_RowStack(stack.RK_RowStack):
    _BOTTOM_IMAGE_ = 'shade'

    def __init__(self, x, y, game, **cap):
        """
        __init__(x, y, game[, offset=(0, YOFFSET), base_suit=4])
          -> None

        :param integer x: left x-coordinate.
        :param integer y: top y-coordinate.
        :param object game: current Game.
        :keyword tuple offset: offset from left and offset from top

        seealso::
          Module pysollib.stackcaps with details of *capability* keywords.
        """
        cap.setdefault('base_suit', 4)
        super(TrumpOnly_RowStack, self).__init__(x, y, game, **cap)


class ShadeSS_RowStack(stack.SS_RowStack):
    _BOTTOM_IMAGE_ = 'shade'


class HighSS_RowStack(HighCardMethod, stack.SS_RowStack):
    """
    Build in rank by same suit. Base KING or high Trump.
    """
    pass